# -*- coding:utf-8 -*-
import unittest
import paramunittest
import readConfig as readConfig
from common import common
from common import configHttp
from common.Log import Logg

#实例化调用
localReadConfig = readConfig.ReadConfig()
localConfigHttp = configHttp.ConfigHttp()
log=Logg();
studentLogin_xls = common.get_xls("userCase.xlsx", "studentLogin")
# [['login_01\n学生端登陆', 'post', '/backend/student/unauth/login', '{\n "phone":14000000000,\n "password":"e10adc3949ba59abbe56e057f20f883e",\n "messageCode":"0",\n "loginType":"1",\n "timestamp":"0"\n}', '0', '200', '操作成功'], ['login_02\n学生端登陆', 'post', '/backend/student/unauth/login', '{\n "phone":14050000000,\n "password":"e10adc3949ba59abbe56e057f20f883e",\n "messageCode":"0",\n "loginType":"1",\n "timestamp":"0"\n}', '0', '200', '操作成功'], ['login_03\n学生端输入\n错误手机号登陆', 'post', '/backend/student/unauth/login', '{\n "phone":140,\n "password":"e10adc3949ba59abbe56e057f20f883e",\n "messageCode":"0",\n "loginType":"1",\n "timestamp":"0"\n}', '1', '508', '手机号输入不正确'], ['login_04\n学生端输入\n错误手机号登陆', 'post', '/backend/student/unauth/login', '{\n "phone":143,\n "password":"e10adc3949ba59abbe56e057f20f883e",\n "messageCode":"0",\n "loginType":"1",\n "timestamp":"0"\n}', '1', '508', '
#参数化
@paramunittest.parametrized(*studentLogin_xls)
class StudentLogin(unittest.TestCase):
    global log, logger, resultPath
    #global把下面运行的方法，部分值放到全局，如果下面使用了，那么我们就传它
    def setParameters(self, case_name, method, url, parameter, resultType, errno, error):
        #得到excel值初始化赋值
        self.case_name = str(case_name)
        self.method = str(method)
        self.url = str(url)
        self.parameter = str(parameter)
        self.resultType = str(resultType)
        self.errno = str(errno)
        self.error = str(error)

    #核心的CASE
    def test_login(self):
        '''学生端手机号登录'''
        localConfigHttp.set_url(self.url) #/backend/student/unauth/login
        header = {"Content-Type": 'application/json'}
        localConfigHttp.set_headers(header)
        localConfigHttp.set_data(self.parameter)
        # 请求对应封装接口
        self.respon = localConfigHttp.postWithJson()
        # 调用checkResult方法
        self.checkResult();

    def setUp(self):
        # 初始化
        pass
        # logger = log.get_logger();
        # logger.error("登陆")

    # 较验json格式返回值
    def checkResult(self):
        # 获取json格式值
        self.info = self.respon.json()
        common.show_return_msg(self.respon)
        # 判断状态是否是200
        if self.resultType == '0' and self.respon.status_code == 200:
            self.assertEqual(self.info['errno'], int(self.errno))
            self.assertEqual(self.info['error'], "操作成功");
        elif self.resultType == '1' and self.respon.status_code == 200:
            self.assertEqual(self.info['errno'], int(self.errno))
            self.assertEqual(self.info['error'], self.error);

if __name__ == '__main__':
    unittest.main()
